﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace TCOChart
{
    public partial class FormPort : UserControl
    {
        public DataInterfacexClass dataInterfacexClass { get; set; }

        public FormPort()
        {
            InitializeComponent();
            string jsonStr2 = @"
               {""_register"":""Alerting..."",
                ""_heartbeat"":""杀杀"",
                ""_state"":""wwwwqqqqqq"",
                ""_controlParam"":""dcddddddd"",
                ""_procParam"":""ss"",
                ""_prodIndiction"" : ""ddddddddd""} ";

            string path = Path.Combine(Environment.CurrentDirectory, "data.json");
            string jsonStr3 = "";
            if (File.Exists(path))
            {
                jsonStr3 = File.ReadAllText(path);
            }

            dataInterfacexClass = DataInterfacexClass.FromJson(jsonStr3);

            input_register.Text = dataInterfacexClass.Register;
            input_heartbeat.Text = dataInterfacexClass.Heartbeat;
            input_status.Text = dataInterfacexClass.State;
            input_controlParam.Text = dataInterfacexClass.ControlParam;
            input_processParam.Text = dataInterfacexClass.ProcParam;
            input_proStandard.Text = dataInterfacexClass.ProdIndiction;
        }

        private void button_Save_Click(object sender, EventArgs e)
        {
            dataInterfacexClass.Register = input_register.Text;
            dataInterfacexClass.Heartbeat = input_heartbeat.Text;
            dataInterfacexClass.State = input_status.Text;
            dataInterfacexClass.ControlParam = input_controlParam.Text;
            dataInterfacexClass.ProcParam = input_processParam.Text;
            dataInterfacexClass.ProdIndiction = input_proStandard.Text;

            // 将对象序列化为 JSON 字符串
            string jsonStr = JsonConvert.SerializeObject(dataInterfacexClass, Formatting.Indented);

            // 保存到文件
            string filePath = "data.json"; // 文件路径
            File.WriteAllText(filePath, jsonStr);
            //AntdUI.Modal.open(new AntdUI.Modal.Config(this, "保存成功", new AntdUI.Modal.TextLine[] {
            //        new AntdUI.Modal.TextLine("数据保存成功")
            //        }, AntdUI.TType.Success)
            //{
            //    CancelText = null,
            //    OkType = AntdUI.TTypeMini.Success,
            //    OkText = "OK",
                
            //});
        }
    }


    public class DataInterfacexClass
    {
        public string Register { get; set; }
        public string Heartbeat { get; set; }
        public string State { get; set; }
        public string ControlParam { get; set; }
        public string ProcParam { get; set; }
        public string ProdIndiction { get; set; }


        public DataInterfacexClass(string register, string heartbeat, string state, string controlParam, string procParam, string prodIndiction)
        {
            Register = register;
            Heartbeat = heartbeat;
            State = state;
            ControlParam = controlParam;
            ProcParam = procParam;
            ProdIndiction = prodIndiction;
        }

        public void Display()
        {
            //Console.WriteLine("Input: " + string.Join(", ", Input));
        }

        public static DataInterfacexClass FromJson(string jsonStr)
        {
            try
            {
                var data = JObject.Parse(jsonStr);
                var register = data["Register"].ToString();
                var heartbeat = data["Heartbeat"].ToString();
                var state = data["State"].ToString();
                var controlParam = data["ControlParam"].ToString();
                var procParam = data["ProcParam"].ToString();
                var prodIndiction = data["ProdIndiction"].ToString();
                return new DataInterfacexClass(register, heartbeat, state, controlParam, procParam, prodIndiction);
            }
            catch (JsonReaderException e)
            {
                return new DataInterfacexClass( "", "", "", "", "", "");
            }
        }
    }
}
